package org.sl.flvplayer.data {
	import org.sl.flvplayer.helper.MathHelper;

	/**
	 * Provides information contained in the flashVars or the playlist xml file concerning the configuration of the player.
	 * 
	 * @package org.sl.flvplayer.data
	 * @author Dan
	 * @version 
	 * @see org.sl.flvplayer.data.InitData
	 * @see org.sl.flvplayer.elements.ControllerElement
	 * @see org.sl.flvplayer.controls.PlayerLogic
	 * @see org.sl.flvplayer.controls.JavaScriptController
	 * @see org.sl.flvplayer.helper.MathHelper
	 */ 
	public class Config {

		private static var instance : Config;

		/**
		 * sets the player in playlistmode
		 */
		public var playlistMode : Boolean = false;

		/**
		 * to distinguish multiple players when in playlistmode
		 */
		public var player_id : String;

		/**
		 * shows the GUI
		 */
		public var showControls : Boolean = true;

		/**
		 * color of the buttons on mouseover, default is 0x333333
		 */
		public var highlightcolor : Number = 0x333333;

		/**
		 * allows the fullscreenmode, default is true
		 */
		public var allowFullscreen : Boolean = true;

		/**
		 * shows the playlist, default is false
		 */
		public var showPlaylist : Boolean = false;

		/**
		 * jumps to the next video, default is false
		 */
		public var autoNext : Boolean = false;

		/**
		 * starts playback without user interaction, default is false
		 */
		public var autoPlay : Boolean = false;
		
		/**
		 * indicates whether the video will be streamed via progressive or other streaming methods like php pseudostreaming, default is false
		 */
		public var streamingMode : Boolean = false;
		
		/**
		 * indicates whether the size of the prieview image scaled to the size of the video, default is true
		 */
		public var scalePreviewImageToVideoSize : Boolean = true;
		
		/**
		 * path to the server, default is null
		 */
		public var serverURL : String = null;

		/**
		 * Ensures that there is only one instance of the class by using the singleton pattern.
		 *
		 * @return instance of the <code>Config</code> class 
		 */
		public static function getInstance() : Config {
			if(!instance) instance = new Config();
			return instance;
		}

		/**
		 * Ensures that all boolean true values from their respective parameternames in the xml object are set to lowercase.
		 * 
		 * @param p_config config xml node
		 */
		public function init(p_config : XML) : void {
			for each (var node : XML in p_config.children()) {
				switch(node.name().toString()) {
					case "playlistMode":
						playlistMode = String(node.children()[0]).toLowerCase() == "true";
						if (playlistMode) player_id = node.@player_id;
						break;
						
					case "showControls":
						showControls = String(node.children()[0]).toLowerCase() == "true";
						if (showControls) highlightcolor = MathHelper.htmlHexToNumber(node.@highlightcolor);
						break;
						
					case "allowFullscreen":
						allowFullscreen = String(node.children()[0]).toLowerCase() == "true";
						break;
						
					case "showPlaylist":
						showPlaylist = String(node.children()[0]).toLowerCase() == "true";
						break;
					
					case "autoNext":
						autoNext = String(node.children()[0]).toLowerCase() == "true";
						break;
						
					case "autoPlay":
						autoPlay = String(node.children()[0]).toLowerCase() == "true";
						break;
						
					case "streamingMode":
						streamingMode = String(node.children()[0]).toLowerCase() == "true";
						if(streamingMode) serverURL = node.@serverURL;
						break;
						
					case "scalePreviewImageToVideoSize":
						scalePreviewImageToVideoSize = String(node.children()[0]).toLowerCase() == "true";
						break;
				}
			}
		}

		/**
		 * Searches for the desired parameternames in the parsed object and sets their boolean false value to uppercase.
		 * 
		 *@param p_paramName name of the flashVar
		 *@param p_paramValue value of the corresponding flashVar
		 */
		public function setParam(p_paramName : String,p_paramValue : String) : void {
			if(p_paramName == "player_id") {
				player_id = p_paramValue;
			} else if("playlistMode,showControls,allowFullscreen,showPlaylist,autoNext,autoPlay,streamingMode,scalePreviewImageToVideoSize".indexOf(p_paramName) > -1) {
				this[p_paramName] = (p_paramValue.toUpperCase() != "FALSE");
			} else if(p_paramName == "highlightcolor") {
				highlightcolor = MathHelper.htmlHexToNumber(p_paramValue);
			} else if(p_paramName == "streamingMode") {
				serverURL = p_paramValue;
			}
		}
	}
}
